package com.ckm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ckm.entity.Classes;
import com.ckm.entity.Student;
import com.ckm.exception.BusinessException;
import com.ckm.mapper.ClassesMapper;
import com.ckm.mapper.StudentMapper;
import com.ckm.service.StudentService;
import com.ckm.util.FindObjectUtil;
import com.ckm.vo.LoginUserVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ClassesMapper classesMapper;
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    @Override
    public String login(LoginUserVo loginUserVo) {
        if (loginUserVo.getId()==null&&loginUserVo.getName()==null){
            throw new BusinessException(250,"账号不能为空");
        }
        if (!loginUserVo.getPassword().equals(loginUserVo.getRePassword())) {
            throw new BusinessException(250,"两次输入密码不一致");
        }
        Student student;
        if (loginUserVo.getId()!=null){
            student = studentMapper.selectById(loginUserVo.getId());
        }else {
            student = studentMapper.selectOne(new QueryWrapper<Student>().eq("name", loginUserVo.getName()));
        }
        if (student == null) {
            throw new BusinessException(250,"账号或密码错误");
        }
        if (!student.getPassword().equals(loginUserVo.getRePassword())) {
            throw new BusinessException(250,"用户名密码错误");
        }
        String token = UUID.randomUUID().toString().replace("-", "");
        redisTemplate.opsForValue().set(token,student, 30, TimeUnit.MINUTES);
        return token;
    }

    @Override
    public Student getStudent(String token) {
        if (Boolean.FALSE.equals(redisTemplate.hasKey(token))){
            throw new BusinessException(250,"无此信息");
        }
        return (Student) redisTemplate.opsForValue().get(token);
    }

    @Override
    public void addStudent(List<Student> studentList) {
        if (studentList == null || studentList.isEmpty()) {
            throw new BusinessException(250,"添加数据集为空");
        }
        for (Student student : studentList) {
            if (!findStudent(student).isEmpty()){
                throw new BusinessException(250,"数据重复："+student);
            }
            if (classesMapper.selectById(student.getClassId()) == null){
                throw new BusinessException(250,"无法找到班级："+student.getClassId());
            }
            studentMapper.insert(student);
        }
        log.info("添加数据：{}", studentList);
    }

    @Override
    public void deleteStudent(Integer id) {
        if (id==null){
            throw new BusinessException(250,"删除参数为空");
        }
        if (studentMapper.selectById(id)==null){
            throw new BusinessException(250,"无此学生");
        }
        studentMapper.deleteById(id);
    }

    @Override
    public void updateStudent(Student student) {
        if (student.getId()==null){
            throw new BusinessException(250,"更新信息缺少id");
        }
        Student select = studentMapper.selectById(student.getId());
        if (select==null){
            throw new BusinessException(250,"无此学生");
        }
        if (select.equals(student)){
            throw new BusinessException(250,"信息无更改");
        }
        studentMapper.updateById(student);
    }

    @Override
    public PageInfo<Student> getStudentPage(Student student, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(findStudent(student));
    }

    @Override
    public List<Student> findStudent(Student student) {
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        FindObjectUtil.fullQueryWrapper(student,wrapper);
        return studentMapper.selectList(wrapper);
    }

    @Override
    public List<Student> findStudentByClassId(Integer classId) {
        Classes classes = classesMapper.selectById(classId);
        if (classes==null){
            throw new BusinessException(250,"无此班级");
        }
        return studentMapper.selectList(new QueryWrapper<Student>().eq("class_id", classId));
    }
}
